
package uestc.bitman.edxapp.downloaded;

import uestc.bitman.edxapp.EdxAppConfigurations;
import uestc.bitman.edxapp.R;

import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class VideoManager extends BaseExpandableListAdapter {

    private Context context = null;

    private List<String> coureseNames = null;

    private List<List<String>> videoNames = null;

    private GetVideo getVideo = null;

    private String rootPath = null;

    private OnClickListener deleteListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            
            String nameString = (String) v.getTag();
            SetVideo setVideo = new SetVideo(Environment.getExternalStorageDirectory()
                    + EdxAppConfigurations.DOWNLOAD_PATH);
            GetVideo getVideo = new GetVideo(Environment.getExternalStorageDirectory()
                    + EdxAppConfigurations.DOWNLOAD_PATH);
            setVideo.deleteVideo(Environment.getExternalStorageDirectory()
                    + EdxAppConfigurations.DOWNLOAD_PATH, getVideo.getVideoPath(nameString));
            notifyDataSetChanged();
        }
    };

    public VideoManager(Context context, String rootPath) {
        this.context = context;
        this.rootPath = rootPath;
        loadVideoInformation();
    }

    private void loadVideoInformation() {
        this.getVideo = new GetVideo(rootPath);
        coureseNames = getVideo.getSameCourse();
        videoNames = getVideo.getSameVideo();
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        return videoNames.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {

        TextView textView = null;
        ImageView imgView = null;
        String videoName = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.downloaded_item, null);
        }
        textView = (TextView) convertView.findViewById(R.id.video_name_view);
        imgView = (ImageView) convertView.findViewById(R.id.video_img_view);

        imgView.setOnClickListener(deleteListener);

        videoName = getChild(groupPosition, childPosition);
        textView.setText(videoName);
        convertView.setTag(getChild(groupPosition, childPosition));
        imgView.setTag(getChild(groupPosition, childPosition));
        textView.setTag("child");
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return videoNames.get(groupPosition).size();
    }

    @Override
    public String getGroup(int groupPosition) {

        return coureseNames.get(groupPosition);
    }

    @Override
    public int getGroupCount() {

        return coureseNames.size();
    }

    @Override
    public long getGroupId(int groupPosition) {

        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {

        TextView textView = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.chapter_item, null);
        }
        textView = (TextView) convertView.findViewById(R.id.chapter_text_view);
        if (coureseNames.isEmpty()) {
            textView.setText("empty");
        } else {
            String courseName;
            courseName = getGroup(groupPosition);
            textView.setText(courseName);
        }
        convertView.setTag("parent");
        textView.setTag("parent");

        return convertView;
    }

    @Override
    public boolean hasStableIds() {

        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public void notifyDataSetChanged() {

        loadVideoInformation();
        super.notifyDataSetChanged();
    }

}
